﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using RaidTeamManager.Model;
using System.Linq;
using wow_api;

namespace RaidTeamManager.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;
        public IRaidManagerRepository Repository { get; set; }
        /// <summary>
        /// The <see cref="WelcomeTitle" /> property's name.
        /// </summary>
        public const string WelcomeTitlePropertyName = "Raid Team Manager";

        private string _welcomeTitle = string.Empty;
        private string _realmName;
        private string _characterName;
        private RelayCommand _findCommand;

        /// <summary>
        /// Gets the WelcomeTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string WelcomeTitle
        {
            get
            {
                return _welcomeTitle;
            }

            set
            {
                if (_welcomeTitle == value)
                {
                    return;
                }

                _welcomeTitle = value;
                RaisePropertyChanged(WelcomeTitlePropertyName);
            }
        }

        public string RealmName
        {
            get { return _realmName; }
            set { _realmName = value; RaisePropertyChanged(RealmName); }
        }

        public string CharacterName
        {
            get { return _characterName; }
            set { _characterName = value; RaisePropertyChanged(CharacterName);}
        }

        public ObservableCollection<RaiderInfo> Raiders { get; set; }

        public RelayCommand FindCommand
        {
            get { return _findCommand ?? (_findCommand = new RelayCommand(FindCharacter)); }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;
            _dataService.GetData(
                (item, error) =>
                {
                    if (error != null)
                    {
                        // Report error here
                        return;
                    }

                    WelcomeTitle = item.Title;
                    Raiders = new ObservableCollection<RaiderInfo>();
                    RealmName = item.DefaultRealm;
                    foreach (var raider in item.Raiders)
                    {
                        Raiders.Add(raider);
                    }
                }); 
            Repository = new EFRaidManagerRepository();
        }

        private void FindCharacter()
        {
            var armory = new Armory();
            var character = armory.GetCharacter(RealmName, CharacterName);
            if (character != null)
            {
                var raider = new RaiderInfo(){CharacterName = character.Name, RealmName = character.Realm, AverageItemLevel = character.Items.AverageItemLevelEquipped};
                raider.Class = Repository.GetClass(character.Class);
                Raiders.Add(raider);
                Repository.AddRaider(raider);
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
        public void UpdateClasses()
        {
            var armory = new Armory();
            var armoryClasses = armory.GetClasses().ClassList;
            var databaseClasses = Repository.GetClasses();
            var rejectList = armoryClasses.Where(i => databaseClasses.ContainsKey(i.Id));
            var addList = armoryClasses.Except(rejectList);
            var classesToAdd = addList.ToDictionary(c => c.Id, c => c.Name);
            Repository.AddClasses(classesToAdd);
        }
    }
}